package com.ruoyi.food.service.impl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.environment.domain.OpmEnvRule;
import com.ruoyi.food.domain.OpmFoodSafePublic;
import com.ruoyi.food.mapper.OpmFoodSafePublicMapper;
import com.ruoyi.food.service.IOpmFoodSafePublicService;
import com.ruoyi.system.api.LinkFileServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 食品宣传Service业务层处理
 *
 * @author cjie
 * @date 2021-07-24
 */
@Service
public class OpmFoodSafePublicServiceImpl extends LinkFileServiceImpl implements IOpmFoodSafePublicService {
    @Autowired
    private OpmFoodSafePublicMapper opmFoodSafePublicMapper;

    /**
     * 查询食品宣传
     *
     * @param id 食品宣传ID
     * @return 食品宣传
     */
    @Override
    public OpmFoodSafePublic selectOpmFoodSafePublicById(Long id) {
        OpmFoodSafePublic result = opmFoodSafePublicMapper.selectOpmFoodSafePublicById(id);
        result.setFileList(getFileList(id.toString()));
        return result;
    }

    /**
     * 查询食品宣传列表
     *
     * @param opmFoodSafePublic 食品宣传
     * @return 食品宣传
     */
    @Override
    public List<OpmFoodSafePublic> selectOpmFoodSafePublicList(OpmFoodSafePublic opmFoodSafePublic) {
        return opmFoodSafePublicMapper.selectOpmFoodSafePublicList(opmFoodSafePublic);
    }

    /**
     * 新增食品宣传
     *
     * @param opmFoodSafePublic 食品宣传
     * @return 结果
     */
    @Override
    public int insertOpmFoodSafePublic(OpmFoodSafePublic opmFoodSafePublic) {
        opmFoodSafePublic.setId(IdUtil.getSnowflake(0, 0).nextId());
        opmFoodSafePublic.setCreateTime(DateUtils.getNowDate());
        opmFoodSafePublic.setCreateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafePublic.getFileIdList(), opmFoodSafePublic.getId().toString());
        return opmFoodSafePublicMapper.insertOpmFoodSafePublic(opmFoodSafePublic);
    }

    /**
     * 修改食品宣传
     *
     * @param opmFoodSafePublic 食品宣传
     * @return 结果
     */
    @Override
    public int updateOpmFoodSafePublic(OpmFoodSafePublic opmFoodSafePublic) {
        opmFoodSafePublic.setUpdateTime(DateUtils.getNowDate());
        opmFoodSafePublic.setUpdateBy(SecurityUtils.getUsername());
        linkFile(opmFoodSafePublic.getFileIdList(), opmFoodSafePublic.getId().toString());
        return opmFoodSafePublicMapper.updateOpmFoodSafePublic(opmFoodSafePublic);
    }

    /**
     * 批量删除食品宣传
     *
     * @param ids 需要删除的食品宣传ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafePublicByIds(Long[] ids) {
        String[] res = new String[ids.length];
        for(int i=0;i<ids.length;i++){
            res[i] = String.valueOf(ids[i]);
        }
        deleteLinkFile(res);
        return opmFoodSafePublicMapper.deleteOpmFoodSafePublicByIds(ids);
    }

    /**
     * 删除食品宣传信息
     *
     * @param id 食品宣传ID
     * @return 结果
     */
    @Override
    public int deleteOpmFoodSafePublicById(Long id) {
        return opmFoodSafePublicMapper.deleteOpmFoodSafePublicById(id);
    }
}
